package cn.jc.spider.neww.main;

import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.collections.CollectionUtils;
import org.apache.torque.TorqueException;
import org.apache.torque.linkage.SpiderJcMapInit;
import org.apache.torque.util.Criteria;

import cn.jc.spider.neww.ParserI;
import cn.jc.spider.neww.ParserImpl_jsoup;
import cn.jc.spider.po.DataXiaohua;
import cn.jc.spider.po.DataXiaohuaPeer;
import cn.jc.spider.po.Parser;
import cn.jc.spider.po.ParserPeer;
import cn.jc.spider.po.Task;
import cn.jc.spider.po.TaskPeer;
import cn.jc.spider.util.HttpTools;

public class XiaoHua_remove {

	/**
	 * @param args
	 * @author JiangChi
	 */
	public static void main(String[] args) {
		SpiderJcMapInit.init_my();
		getdatafromxiaohua(1,7,30);
	}
	private static int timetime = 0;//注意这个变量的使用
	private static void getdatafromxiaohua(int from, int to, int time) {
		for (int page = from; page <= to; page++) {
			System.out.println("正在请求第"+page+"页数据");
			String url;
			if (page > 1) {
				url = "http://www.kl688.com/newjokes/index_" + page + ".htm";
			} else {
				url = "http://www.kl688.com/index.htm";
			}
			if (timetime > time) {
				System.out.println("发现了"+timetime+"条重复数据，请求终止");
				break;
			}
			getonetime(8, url, "GBK");
			try {
				Thread.sleep(5*1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	private static void getonetime(int taskid, String url, String encode) {
		List<Task> ts = null;
		Criteria c = new Criteria();
		try {
			c.add(TaskPeer.ID, taskid);
			ts = TaskPeer.doSelect(c);
		} catch (TorqueException e) {
			e.printStackTrace();
			System.out.println("查询Task异常 sql:" + c.toString());
		}
		Task task = null;
		if (!CollectionUtils.isEmpty(ts)) {
			task = ts.get(0);
		}
		Criteria cc = new Criteria();
		List<Parser> ps = null;
		try {
			cc.add(ParserPeer.ID_TASK, task.getId());
			ps = ParserPeer.doSelect(cc);
		} catch (TorqueException e) {
			e.printStackTrace();
			System.out.println("查询Task异常 sql:" + cc.toString());
		}
		ParserI parser = new ParserImpl_jsoup(task, ps);
		String content = HttpTools.getContentFromUrlByGet(url, null, null,
				encode);
		// System.out.println(content);
		Map<String, Object> o = parser.parseToMap(content);
		List<Map<String, Object>> ml = (List<Map<String, Object>>) o
				.get("list");
		for (Map<String, Object> m : ml) {
			System.out.println(m);
			Criteria cccc = buildCriteria_for_wangpiao(
					DataXiaohuaPeer.TABLE_NAME, m);
			try {
				Criteria ccc = new Criteria();
				ccc.add(DataXiaohuaPeer.URL, m.get("url"));
				List<DataXiaohua> dxs = DataXiaohuaPeer.doSelect(ccc);
				if (CollectionUtils.isEmpty(dxs)) {
					DataXiaohuaPeer.doInsert(cccc);
				} else {
					timetime++;
				}
			} catch (TorqueException e) {
				e.printStackTrace();
			}
		}
		JSONObject json = JSONObject.fromObject(o);
		// System.out.println(json.toString());
	}
	private static Criteria buildCriteria_for_wangpiao(String tbName,
			Map<String, Object> map) {
		Criteria criteria = new Criteria();
		for (String key : map.keySet()) {
			Object value = map.get(key) + "";
			if ("false".equals(value)) {
				value = 0;
			} else if ("true".equals(value)) {
				value = 1;
			}
			String key_tmp = key;
			if ("SpeicalDes".equals(key)) {
				key_tmp = "SpecialDes";
			}
			criteria.add(tbName + "." + key_tmp, value);
		}
		return criteria;
	}

}
